package com.lgx.mall.business.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.alipay.util.UtilDate;
import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.business.dao.IRechargeInfoDao;
import com.lgx.mall.business.entity.RechargeInfo;

/**
 * 
 * @author acer
 *
 */
@Component("rechargeInfoDao")
public class RechargeInfoDao extends GeneralDao implements IRechargeInfoDao {

	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}
	
	@Transactional
	public RechargeInfo createRechargeInfo(RechargeInfo rechargeInfo) throws DataException {
		//生成订单号
		String orderNo = UtilDate.getOrderNum();
		rechargeInfo.setOrderNo(orderNo);
		
		this.getHibernateTemplate().save(rechargeInfo);
		log.info("[创建商家充值]:"+rechargeInfo.getId());
		return rechargeInfo;
	}

	@Transactional
	public RechargeInfo updateRechargeInfo(RechargeInfo rechargeInfo) throws DataException {
		this.getHibernateTemplate().update(rechargeInfo);
		log.info("[更新商家充值]:"+rechargeInfo.getId());
		return rechargeInfo;
	}

	@Transactional
	public void deleteRechargeInfo(RechargeInfo rechargeInfo) throws DataException {
		log.info("[删除商家充值]:"+rechargeInfo.getId());
		this.getHibernateTemplate().delete(rechargeInfo);
	}

	public void deleteRechargeInfo(long id) throws DataException {
		this.deleteRechargeInfo(this.findRechargeInfoById(id));
		log.info("[删除商家充值]:"+id);
	}

	public RechargeInfo findRechargeInfoById(long id) {
		return this.getHibernateTemplate().get(RechargeInfo.class, id);
	}
	
	public Pagination findRechargeInfoPagination(Object[] params, int pageSize, int pageNum){
		String sql = "from RechargeInfo r ";
		
		sql += " where 1=1";
		
		if(params[0] != null){
			if(params[1] != null){
				sql += " and unix_timestamp(r.rechargeDate) between unix_timestamp('"+params[0]+"') and unix_timestamp('"+params[1]+"')";
			}else{
				sql += " and unix_timestamp(r.rechargeDate) > unix_timestamp('"+params[0]+"'))";
			}
		}else{
			if(params[1] != null){
				sql += " and unix_timestamp(r.rechargeDate) < unix_timestamp('"+params[1]+"'))";
			}
		}
		
		if(params[2] != null){
			if(params[3] != null){
				sql += " and unix_timestamp(r.fixDate) between unix_timestamp('"+params[2]+"') and unix_timestamp('"+params[3]+"')";
			}else{
				sql += " and unix_timestamp(r.fixDate) > unix_timestamp('"+params[2]+"')";
			}
		}else{
			if(params[3] != null){
				sql += " and unix_timestamp(r.fixDate) < unix_timestamp('"+params[3]+"')";
			}
		}
		
		if(params[4] != null){
			sql += " and r.status = "+params[4];
		}
		
		sql += " order by r.id asc";
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}
	
	public void deleteRechargeInfos(String[] ids) throws DataException {
		for(String id : ids)
		{
			this.deleteRechargeInfo(Long.parseLong(id));
		}
	}

	@Override
	public RechargeInfo findRechargeInfoByOrderNo(String orderNo) {
		String hql = "from RechargeInfo r where r.orderNo = '"+orderNo+"'";
		
		@SuppressWarnings("unchecked")
		List<RechargeInfo> rs = this.getHibernateTemplate().find(hql);
		
		if(rs.size() > 0){
			return rs.get(0);
		}
		 
		return null;
	}
}
